package logic

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"log"
	"user/databases"
	"user/model"

	"user/internal/svc"
	"user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateUserStarGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateUserStarGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserStarGoodLogic {
	return &CreateUserStarGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateUserStarGoodLogic) CreateUserStarGood(in *user.CreateUserStarGoodRequest) (*user.CreateUserStarGoodResponse, error) {
	// todo: add your logic here and delete this line
	if in.Info == nil {
		log.Println("info is required")
		return nil, status.Error(codes.InvalidArgument, "info is required")
	}
	if in.Info.UserID == 0 {
		log.Println("user_id is required")
		return nil, status.Error(codes.InvalidArgument, "user_id is required")
	}
	if in.Info.GoodSKUID == 0 {
		log.Println("user_id is required")
		return nil, status.Error(codes.InvalidArgument, "good_sku_id is required")
	}

	userStarGood := model.UserStarGood{
		UserID:    in.Info.UserID,
		GoodSKUID: in.Info.GoodSKUID,
	}
	err := databases.DB.Create(&userStarGood).Error
	if err != nil {
		return nil, status.Error(codes.Internal, err.Error())
	}

	return &user.CreateUserStarGoodResponse{
		Info: &user.UserStartGood{
			ID:        int64(userStarGood.ID),
			GoodSKUID: userStarGood.GoodSKUID,
			UserID:    userStarGood.UserID,
		},
	}, nil
}
